<?php
namespace app\home\validate;
use think\Validate;

class Login extends Validate
{
    protected $rule = [
        'name' => 'require',
        'mobile' => 'require|length:11|checkmobile:1',
        'id_card' => 'require|length:18|checkIdCard:1'
    ];

    protected $message = [
        'name.require' => '用户名必须填写',
        'mobile.require' => '手机号必须填写',
        'mobile.length' => '手机号必须11个字符',
        'mobile.checkmobile' => '手机号格式不正确',
        'id_card.checkIdCard' => '身份证号格式不正确',
        'id_card.require' => '身份证号必须填写',
        'id_card.length' => '身份证号必须18位'
    ];

    /*手机号验证*/
    protected function checkmobile($value)
    {
        $res = preg_match("/^[1][3,4,5,6,7,8,9][0-9]{9}$/", $value, $matches);
        if (!$res) {
            return false;
        }
        return true;
    }

    /*身份证验证*/
    protected function checkIdCard($value)
    {
        $res = preg_match("/^[0-9]{17}([0-9]|X|x)$/", $value, $matches);
        if (!$res) {
            return false;
        }
        return true;
    }
}